#!/usr/bin/env perl

use strict;
BEGIN {
	if(!exists($::ENV{LINUXHA_LIBS})) {
		$::ENV{LINUXHA_LIBS}="/opt/linuxha14/lib/perl";
	}
	push @INC,$::ENV{LINUXHA_LIBS};
}

use libping6;;
 
print "Loaded libping6 ok...\n";

use Data::Dumper;
use ipinterfaces;

my $ip=new ipinterfaces();
$ip->scan_interfaces; print Dumper($ip);

# list tests
{
	my @t=$ip->list(physical=>1);
	if(@t) {
		printf("Physical interfaces (no logical cards should be shown): %s\n",join(",",@t));
	} else {
		printf("No physical information returned!\n");
	}

	my ($i4,$i6)=$ip->list(logical=>1);
	printf("ipV4 logical interfaces: %s\n",join(",",@{$i4}));
	printf("ipV6 logical interfaces: %s\n",join(",",@{$i6}));
}

# get_logical tests... Returns the 'id' of the next interface - 
# though of course this is no longer actually directly relevant!

{
	my $t1=$ip->get_logical(interface => "eth0");
	my $t2=$ip->get_logical(interface => "br0");
	printf("get_logical for eth0 = $t1\n");
	printf("get_logical for br0 = $t2\n");
}

# Do some IPv6 address expansion tests...
{
	my $a=$ip->exp_ipv6_addr("::1");
	printf("Expansion of '::1' is %s\n",$a);

	my ($b,$m)=$ip->exp_ipv6_addr("fe80::1e6f:65ff:fe30:7123/64");
	printf("Expansion of 'fe80::1e6f:65ff:fe30:7123/64' is %s / %d\n",$b,$m);

	($b,$m)=$ip->exp_ipv6_addr("1:2:3::4/64");
	printf("Expansion of '1:2:3::4/64' is %s / %d\n",$b,$m);
}

# Do some get addr tests
{
	my $ipv4_addr="192.168.1.102";
	my $ipv6_addr="IP6|fe80::1e6f:65ff:fe30:7123";
	my $ipv4_addr2="127.0.0.2";
	my $i1=$ip->get_interface(ip=>$ipv4_addr);
	my $i2=$ip->get_interface(ip=>$ipv6_addr);
	my $i3=$ip->get_interface(ip=>$ipv4_addr2);
	printf("Interace for ip of $ipv4_addr is: $i1\n");
	printf("Interace for ip of $ipv4_addr2 is: $i3\n");
	printf("Interace for ip of $ipv6_addr is: $i2\n");
}

# Do some have link tests
{
	my ($a,$b)=$ip->have_link(interface => "fred");
	print "have_link fred   : ($a,$b)\n";
	($a,$b)=$ip->have_link(interface => "lo:0");
	print "have_link lo:0   : ($a,$b)\n";
	($a,$b)=$ip->have_link(interface => "br0:0");
	print "have_link br0:0   : ($a,$b)\n";
	($a,$b)=$ip->have_link(interface => "eth0:0");
	print "have_link eth0:0   : ($a,$b)\n";
}

# Do some ip_check tests
{
	my ($a,$b)=$ip->ip_check(ips=>"127.0.0.1");
	print "ip_check of 127.0.0.1 = $a,$b\n";
	($a,$b)=$ip->ip_check(ips=>"IP6|::1");
	print "ip_check of ::1 = $a,$b\n";
}

# Do a get_details2 test finally
{
	my $r=$ip->get_details2(interface=>"fred:1");
	$r=$ip->get_details2(interface=>"eth0:1");
	print "det_details2 for eth0:1=",Dumper($r);
	$r=$ip->get_details2(interface=>"eth0:0");
	print "det_details2 for eth0:0=",Dumper($r);
}

